package com.anbow.courseware.controller;

import com.anbow.courseware.entity.DTO.TeachingQuestionsDTO;
import com.anbow.courseware.entity.DTO.VipTeachingQuestionsDTO;
import com.anbow.courseware.entity.FORM.TeachingQuestionsForm;
import com.anbow.courseware.entity.VO.TeachingQuestionsVO;
import com.anbow.courseware.service.impl.TeachingQuestionsServiceImpl;
import com.anbow.courseware.service.impl.VipTeachingQuestionsServiceImpl;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author zhaoYuFei
 */
@Controller
@RequestMapping("/teachingQuestions")
public class TeachingQuestionsController {
    @Autowired
    private TeachingQuestionsServiceImpl teachingQuestionsService;
    @Autowired
    private VipTeachingQuestionsServiceImpl vipTeachingQuestionsService;

    @GetMapping("/getByLabel")
    public String getLabel(@RequestParam(value = "label", required = false) String label,
                           @RequestParam(value = "pageNum", required = false) Integer pageNum, HttpSession session) {
        List<TeachingQuestionsDTO> all = teachingQuestionsService.getByLabel(label, pageNum);
        if (all == null || all.size() == 0) {
            //  return R.ok("成功");
            session.setAttribute("list", null);
        } else {
            if (all == null || all.size() == 0) {
                session.setAttribute("list", all);
            } else {
                List<TeachingQuestionsVO> collect = all.stream().map(e -> {
                    TeachingQuestionsVO teachingQuestionsVO = new TeachingQuestionsVO();
                    BeanUtils.copyProperties(e, teachingQuestionsVO);
                    return teachingQuestionsVO;
                }).collect(Collectors.toList());
                //  return R.ok("成功",collect);
                session.setAttribute("list", collect);
                session.setAttribute("total", collect.size());
            }

        }
        return "insert";
    }


    @GetMapping("/getByLabelToPage")
    public String getLabelToPage(@RequestParam(value = "label", required = false) String label,
                                 @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                 HttpSession session) {
        List<TeachingQuestionsDTO> all = teachingQuestionsService.getByLabel(label, pageNum);
        if (all == null || all.size() == 0) {
            //  return R.ok("成功");
            session.setAttribute("list", null);
        } else {
            if (all == null || all.size() == 0) {
                session.setAttribute("list", all);
            } else {
                List<TeachingQuestionsVO> collect = all.stream().map(e -> {
                    TeachingQuestionsVO teachingQuestionsVO = new TeachingQuestionsVO();
                    BeanUtils.copyProperties(e, teachingQuestionsVO);
                    return teachingQuestionsVO;
                }).collect(Collectors.toList());
                //  return R.ok("成功",collect);
                session.setAttribute("list", collect);
            }

        }
        return "insert";
    }

    @GetMapping("/getById/{id}")
    public String getById(@PathVariable("id") String id, HttpSession session) {
        TeachingQuestionsDTO teachingQuestionsDTO = teachingQuestionsService.getById(id);
        session.setAttribute("updateData", teachingQuestionsDTO);
        session.setAttribute("context", teachingQuestionsDTO.getContext());
        session.setAttribute("id", teachingQuestionsDTO.getId());
        session.setAttribute("title", teachingQuestionsDTO.getTitle());
        session.setAttribute("label", teachingQuestionsDTO.getLabel());
        session.setAttribute("result", teachingQuestionsDTO.getResult());
        session.setAttribute("status", teachingQuestionsDTO.getStatus());
        return "update";
    }

    @PostMapping(value = "/update")
    public String update(@RequestParam(value = "id", required = false) String id, @RequestParam("title") String title,
                         @RequestParam("label") String label,
                         @RequestParam("context") String context, @RequestParam("result") String result,
                         HttpSession session) {
        TeachingQuestionsForm teachingQuestionsForm = new TeachingQuestionsForm();
        teachingQuestionsForm.setContext(context);
        teachingQuestionsForm.setTitle(title);
        teachingQuestionsForm.setLabel(label);
        teachingQuestionsForm.setResult(result);
        if (id == null) {
            teachingQuestionsForm.setCreateUserId((Integer) session.getAttribute("create_user_id"));
        } else {
            teachingQuestionsForm.setId(Integer.valueOf(id));
        }
        teachingQuestionsForm.setUpdateUserId((Integer) session.getAttribute("create_user_id"));
        TeachingQuestionsDTO teachingQuestionsDTO = new TeachingQuestionsDTO();
        BeanUtils.copyProperties(teachingQuestionsForm, teachingQuestionsDTO);

        if ("admin".equals((String) session.getAttribute("username"))) {
            teachingQuestionsDTO.setStatus("1");
            VipTeachingQuestionsDTO vipTeachingQuestionsDTO = new VipTeachingQuestionsDTO();
            BeanUtils.copyProperties(teachingQuestionsDTO, vipTeachingQuestionsDTO);
            vipTeachingQuestionsDTO.setCreateUserId((Integer) session.getAttribute("create_user_id"));
            vipTeachingQuestionsDTO.setCreateTime(LocalDate.now());
            vipTeachingQuestionsDTO.setTqId(teachingQuestionsDTO.getId());
            vipTeachingQuestionsService.add(vipTeachingQuestionsDTO);
        }
        if (teachingQuestionsDTO.getStatus() == null) {
            teachingQuestionsDTO.setStatus("0");
        }
        teachingQuestionsService.update(teachingQuestionsDTO);
        List<TeachingQuestionsDTO> all = teachingQuestionsService.getAll();
        session.setAttribute("list", all);
        return "insert";
    }

    @PostMapping("/add")
    public String add(@RequestParam(value = "id", required = false) String id, @RequestParam("title") String title,
                      @RequestParam("label") String label,
                      @RequestParam("context") String context, @RequestParam("result") String result,
                      HttpSession session) {
        TeachingQuestionsForm teachingQuestionsForm = new TeachingQuestionsForm();
        teachingQuestionsForm.setContext(context);
        teachingQuestionsForm.setTitle(title);
        teachingQuestionsForm.setLabel(label);
        teachingQuestionsForm.setResult(result);
        if (id == null) {
            teachingQuestionsForm.setCreateUserId((Integer) session.getAttribute("create_user_id"));
        } else {
            teachingQuestionsForm.setId(Integer.getInteger(id));
        }
        teachingQuestionsForm.setUpdateUserId((Integer) session.getAttribute("create_user_id"));
        TeachingQuestionsDTO teachingQuestionsDTO = new TeachingQuestionsDTO();
        BeanUtils.copyProperties(teachingQuestionsForm, teachingQuestionsDTO);
        teachingQuestionsDTO.setCreateTime(LocalDate.now());
        teachingQuestionsService.add(teachingQuestionsDTO);
        List<TeachingQuestionsDTO> all = teachingQuestionsService.getAll();
        session.setAttribute("list", all);
        session.setAttribute("total", (int) session.getAttribute("total") + 1);
        return "insert";
    }

    @PostMapping("/delete/{id}")
    public String delete(@PathVariable("id") String id, HttpSession session) {
        teachingQuestionsService.delete(id);
        List<TeachingQuestionsDTO> all = teachingQuestionsService.getAll();
        session.setAttribute("list", all);
        session.setAttribute("total", (int) session.getAttribute("total") - 1);
        return "insert";
    }

}
